package com.example.avplayer;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;

import com.example.avplayer.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private MediaProjectionManager mediaProjectionManager;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;
//    private LocalSurfaceView cameraSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding inflate = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());

//        FrameLayout frameLayout = findViewById(R.id.camera_preview);
//
//        frameLayout.addView(new CameraSurfaceView(this));

//          cameraSurfaceView = findViewById(R.id.camera_preview);

//        SurfaceView surfaceView = findViewById(R.id.surfaceView);
//        SurfaceHolder holder = surfaceView.getHolder();
//        holder.addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(@NonNull SurfaceHolder holder) {
//                Surface surface = holder.getSurface();
//                new H264Player(Environment.getExternalStorageDirectory().getAbsolutePath()+"/out.h264",surface).paly();
//            }
//
//            @Override
//            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
//
//            }
//
//            @Override
//            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
//
//            }
//        });
         intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() != RESULT_OK) return;

                MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(result.getResultCode(), result.getData());

                H264Encoder h264Encode = new H264Encoder(mediaProjection);
                h264Encode.start();
            }
        });

    }

    public void startRecord(View view) {
//        // 开始录制
//        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
//        Intent screenCaptureIntent = mediaProjectionManager.createScreenCaptureIntent();
//        intentActivityResultLauncher.launch(screenCaptureIntent);
//        cameraSurfaceView.s

    }


}